﻿using System;
using System.Collections.Generic;
using System.Text;
using Nemerle.Utility;

namespace Nemerle.Xml
{
  using Namespace;

  /// <summary>
  /// Attribute
  /// </summary>
  [Record]
  public sealed class Attr : IEquatable[Attr]
  {
    [Accessor] ns     : Namespace;
    [Accessor] prefix : string;
    [Accessor] name   : string;
    [Accessor] value  : string;

    public this(name : string, value  : string)
    {
      this(Empty(), "", name, value);
    }

    public override GetHashCode() : int
    {
      Name.GetHashCode() ^ Value.GetHashCode()
    }

    public override Equals(obj : object) : bool
    {
      if (object.ReferenceEquals(this, obj)) true
      else
      {
        if (this.GetType().Equals(obj.GetType()))
        {
          def other = obj :> Attr;
          Ns.Equals(other.Ns)
          && Name == other.Name
          && Value == other.Value
        }
        else
          false
      }
    }

    public Equals(att : Attr) : bool implements IEquatable[Attr].Equals
    {
      Equals(att : object)
    }

    public override ToString() : string
    {
      match (this.Ns)
      {
        | Empty => $<#$Name="$Value"#>
        | _ when (!string.IsNullOrEmpty(this.Prefix)) => $<#$Prefix:$Name="$Value"#>
        | _ => $<#$Name="$Value"#>
      }
    }
  }
}
